import http from '@/util/http'

const module = {
  namespaced: true, // 命名空间
  state: {
    searchNextIndex: 0,
    keyWord: '手机壳',
    searchList: [],
    sort: 0
  },
  actions: {
    getSearchList (store, params) {
      params.sort = params.sort === undefined ? 0 : params.sort
      http.request({
        url: `/api/search?word=${encodeURI(params.sword)}&start=${params.start}&sort=${params.sort}&couponOnly=NaN&minPrice=0&maxPrice=99999`
      }).then(res => {
        store.commit('setSearchList', res.data.data.list)
        store.commit('setSearchNextIndex', res.data.data.nextIndex)
      })
    },
    getSearchMore (store, params) {
      params.sort = params.sort === undefined ? 0 : params.sort
      http.request({
        url: `/api/search?word=${encodeURI(params.sword)}&start=${params.start}&sort=${params.sort}&couponOnly=NaN&minPrice=0&maxPrice=99999`
      }).then(res => {
        store.commit('pushSearchList', res.data.data.list)
        store.commit('setSearchNextIndex', res.data.data.nextIndex)
      })
    }
  },
  mutations: {
    pushSearchList (state, data) {
      console.log('pushSearchList', data)
      state.searchList.push(...data)
    },
    setSearchNextIndex (state, data) {
      console.log('searchNextIndex', data)
      state.searchNextIndex = data
    },
    setSort (state, data) {
      console.log('setSort', data)
      state.sort = data
    },
    setSearchList (state, data) {
      console.log('setSearchList', data)
      state.searchList = data
    },
    setKeyWord (state, data) {
      state.keyWord = data
    }
  }
}

export default module
